<?php
/*
mp3转m4a
convert.php song
安装环境：
wget https://ffmpeg.org/releases/ffmpeg-4.0.tar.bz2
wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
tar -zxvf yasm-1.3.0.tar.gz
cd yasm-1.3.0/
./configure
make
make install
cd ../
tar -xjvf ffmpeg-4.0.tar.bz2 
cd ffmpeg-4.0
./configure --enable-shared --prefix=/usr/local/ffmpeg #这个过程很慢
make
make install
#下一步
vim /etc/ld.so.conf.d/ffmpeg.conf #输入/usr/local/ffmpeg/lib
ldconfig
cd /usr/local/ffmpeg/bin
./ffmpeg -version
export PATH="$PATH:/usr/local/ffmpeg/bin"
export 
文章参考：https://www.cnblogs.com/freeweb/p/6897907.html
*/
function get_files($dir)
{
    $dir = realpath($dir) . "/";
    $files = array();

    if (!is_dir($dir)) {
        return $files;
    }

    $pattern = $dir . "*";
    $file_arr = glob($pattern);

    foreach ($file_arr as $file) {
        if (is_dir($file)) {
            $temp = get_files($file);

            if (is_array($temp)) {
                $files = array_merge($files, $temp);
            }
        } else {
            // print_r($file);
            $files[] = substr(str_replace(__DIR__, "", $file), 1);
        }
    }
    return $files;
} //  end function
// print_r(__DIR__);
$file_list = get_files(__DIR__ . "/" . $argv[1]);
if (!file_exists($argv[1] . "_result")) {
    mkdir($argv[1] . "_result");
}
// print_r($file_list);
foreach ($file_list as $key => $value) {
    if (strstr($value, ".mp3")) {
        // print_r();
        if (!file_exists($paths = __DIR__ . "/" . $argv[1] . "_result/" . pathinfo($value)['dirname'])) {
            mkdir($paths, 0777, true);
        }
        // $convert_path = $paths . basename($value) . PHP_EOL;
        $path_convert = str_replace(".", "", $paths) .'/'.explode('.',basename($value))[0];
        // print_r($path_convert . PHP_EOL);
        // print_r(basename($value) . PHP_EOL);
        shell_exec("ffmpeg -i " . $value . " -c:a aac -vn -ar 32000 -ab 32k -ac 1 -map_metadata -1 " . $path_convert . ".m4a");
        // print_r("convert/" . $value . ".m4a" . PHP_EOL);
    }
}
